package com.fashionista;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * An adapter class for implementing an ArrayAdapter in a ListView to use images and text 
 * 
 * @author Trevor Adelman, Patrick Koperwas, Tong Wu, Shayan Yassami
 * @version for cs48 group project, 3/11/2011
 */

public class ClothingArrayAdapter extends ArrayAdapter<ClothingItems> {
	private static final String tag = "ClothingArrayAdapter";
	private static final String ASSETS_DIR = "images/";
	private Context context;
	private ImageView clothingIcon;
	private TextView clothingName;
	private TextView clothingType;
	private List<ClothingItems> clItems = new ArrayList<ClothingItems>();

	/**
	 * Creates an adapter for the ListView by setting the list of clothing and the context 
	 * 
	 * @param context 
	 * @param textViewResourceId Parameter to pass the xml layout to use for the adapter
	 * @param objects List of clothing items that are gotten from teh parser.
	 */
	public ClothingArrayAdapter(Context context, int textViewResourceId,
			List<ClothingItems> objects) {
		super(context, textViewResourceId, objects);
		this.context = context; //Sets the context (usually to the application context with "getApplicationContext()")
		this.clItems = objects; //Sets the list to object
	}

	/**
	 * Method to retrieve the size of the clothing list
	 * 
	 * @return Size of the list
	 */
	public int getCount() {
		return clItems.size();
	}

	/**
	 * Gets the specific clothing item from the list of ClothingItems.
	 * 
	 * @param index The position of the clothing
	 * @return Clothing item located at index in the list
	 */
	public ClothingItems getItem(int index) {
		return clItems.get(index);
	}

	/**
	 * Method that retrieves the View of a row in the list. This overrides getView in ArrayAdapter
	 * 
	 * @param position The position in the list of the desired item
	 * @param convertView The old view that is to be reused for this method
	 * @param parent The parent ViewGroup to be used for the row
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) { //Checks if convertView is valid, if not, make it with inflate
			Log.d(tag, "Starting XML Row Inflation...");
			LayoutInflater inflater = (LayoutInflater) this.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.wardrobe_listitem, parent, false);
			Log.d(tag, "Complete");
		}

		ClothingItems clothing = getItem(position);
		clothingIcon = (ImageView) row.findViewById(R.id.clothing_icon); //Creates an ImageView and two TextViews for the icon and two text areas
		clothingName = (TextView) row.findViewById(R.id.clothing_name);
		clothingType = (TextView) row.findViewById(R.id.clothing_type);

		clothingName.setText(clothing.name); //Sets the name of the text in clothing name

		String imgFilePath = ASSETS_DIR + clothing.resourceId; //Gets the correct icon image from /assets/images and sets it as clothingIcon
		try {
			Bitmap bitmap = BitmapFactory.decodeStream(this.context
					.getResources().getAssets().open(imgFilePath));
			clothingIcon.setImageBitmap(bitmap);
		} catch (IOException e) {
			e.printStackTrace();
		}

		clothingType.setText(clothing.type); //Sets the clothing type to top/pants/shoes ect.
		return row;
	}

}
